home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-13 | 2.5 KB | 66 lines |
- /*
- * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
- *
- * Permission to use, copy, modify, and distribute this software
- * and its documentation for NON-COMMERCIAL purposes and without
- * fee is hereby granted provided that this copyright notice
- * appears in all copies. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
- * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
- * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
- * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
- import java.io.*;
- import java.net.*;
-
- public class KnockKnockClient {
- public static void main(String[] args) {
- Socket kkSocket = null;
- PrintWriter pw = null;
- BufferedReader br = null;
-
- try {
- kkSocket = new Socket("taranis", 4444);
- pw = new PrintWriter(kkSocket.getOutputStream());
- br = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
- } catch (UnknownHostException e) {
- System.err.println("Don't know about host: taranis");
- } catch (IOException e) {
- System.err.println("Couldn't get I/O for the connection to: taranis");
- }
-
- if (kkSocket != null && pw != null && br != null) {
- try {
- StringBuffer buf = new StringBuffer(50);
- int c;
- String fromServer;
-
- while ((fromServer = br.readLine()) != null) {
- System.out.println("Server: " + fromServer);
- if (fromServer.equals("Bye."))
- break;
- while ((c = System.in.read()) != '\n') {
- buf.append((char)c);
- }
- System.out.println("Client: " + buf);
- pw.println(buf.toString());
- pw.flush();
- buf.setLength(0);
- }
-
- pw.close();
- br.close();
- kkSocket.close();
- } catch (UnknownHostException e) {
- System.err.println("Trying to connect to unknown host: " + e);
- } catch (IOException e) {
- System.err.println("IOException: " + e);
- }
- }
- }
- }
-